package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.PageInfo;
import com.tanhua.dubbo.server.pojo.Questionnaire;
import com.tanhua.dubbo.server.pojo.SoulReport;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/3/4 10:37
 * @version: 0.0.1
 * @modified By:
 */
@Service(version = "1.0.0")
public class SoulReportApiImpl implements SoulReportApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /*@Override
    public String saveReport(SoulReport soulReport) {
        if(null==soulReport.getUserId()){
            return null;
        }
        soulReport.setId(ObjectId.get());
        soulReport.setCreated(System.currentTimeMillis());
        this.mongoTemplate.save(soulReport);
        return soulReport.getId().toHexString();
    }*/

    @Override
    public String saveReport(SoulReport soulReport) {

        if (null == soulReport.getUserId()) {
            return null;
        }

        Query query = Query.query(Criteria.where("userId").is(soulReport.getUserId()).and("testId").is(soulReport.getTestId()));
        SoulReport report = this.mongoTemplate.findOne(query, SoulReport.class);
        if (null == report) {
            soulReport.setId(ObjectId.get());
            soulReport.setCreated(System.currentTimeMillis());
            SoulReport save = this.mongoTemplate.save(soulReport);
            return save.getId().toHexString();
        } else {
            Update update = Update.update("score", soulReport.getScore()).set("cover", soulReport.getCover()).set("conclusion", soulReport.getConclusion());
            this.mongoTemplate.updateFirst(query, update, SoulReport.class);
            return report.getId().toHexString();
        }
    }

    @Override
    public SoulReport queryReportById(String reportId) {
        SoulReport soulReport = this.mongoTemplate.findById(new ObjectId(reportId), SoulReport.class);
        return soulReport;
    }

    @Override
    public PageInfo<SoulReport> queryReportList(Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = new Query().with(pageable);
        List<SoulReport> soulReports = this.mongoTemplate.find(query, SoulReport.class);
        PageInfo<SoulReport> pageInfo = new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(soulReports);
        return pageInfo;
    }

    /**
     * 获取全部的问卷
     *
     * @return
     */
    @Override
    public List<Questionnaire> getFindAll() {
        List<Questionnaire> questionnaireList = this.mongoTemplate.findAll(Questionnaire.class);

        return questionnaireList;
    }

    /**
     * 查询全部问卷
     *
     * @return
     */
    @Override
    public List<Questionnaire> queryAll() {
        List<Questionnaire> all = mongoTemplate.findAll(Questionnaire.class);
        return all;
    }

    /**
     * 通过userId,问卷Id 查询最新报告
     * @param userId
     * @return
     */
    @Override
    public SoulReport queryByUserId(Long userId,String testId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("testId").is(testId)).with(Sort.by(Sort.Order.desc("created")));
        SoulReport one = this.mongoTemplate.findOne(query, SoulReport.class);
        return one;
    }

    /**
     * 查询用户做过的问卷
     *
     * @param userId
     * @return
     */
    @Override
    public List<SoulReport> querySoulReportByUserId(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<SoulReport> soulReports = mongoTemplate.find(query, SoulReport.class);
        return soulReports;
    }
}
